<?php
/**
 * Created by PhpStorm.
 * User: 田家强
 * Date: 2017/4/3
 * Time: 14:12
 */

namespace Home\Controller;
use Think\Controller;


class LoginController extends Controller
{

    public function index()
    {

        $headConf = ['title'=>'维依尚品_登录页面','js'=>['list']];
        $this->assign('headConf',$headConf);


        $Data=$_POST;
        $dbData=m('login')->select();
        if (IS_POST){
            foreach ($dbData as $k=>$v){
                if ($v['username']==$Data['username']&& md5($Data['password'])==$v['password'] && (new \Think\Verify())->check($Data['code'])){
                    session('login',"{$Data['username']}");
                    session('uid',$v['id']);
                   if(CONTROLLER_NAME=='Accounts'){
                        $this->success('登陆成功',u('home/Accounts/index'));exit;
                    } else{
                        dd(222);
                        $this->success('登陆成功',cookie('oldUrl'));exit;
                    }

                }
            }
            $this->error('登录失败,请核实您的用户信息');exit;
        }

        if( cookie('oldControllerName')!='Res'){

            $oldUrl=$_SERVER['HTTP_REFERER'];
            cookie('oldUrl',$oldUrl);
        }else{
            $oldUrl="http://c79_tianjiaqiang.houdunphp.com/shop/index.php";
            cookie('oldControllerName','');
            cookie('oldUrl',$oldUrl);
        }
    $this->display();
    }

    /**
     * 验证码设置方法
     */
    public function code()
    {
        //验证码设置
        $config =    array(
            'fontSize'    =>    30,    // 验证码字体大小
            'length'      =>    3,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
            'imageH'      =>    70,     //验证码高度设置
        );
        //显示验证码 【设置验证码时要传参（$config）】
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    // 检测输入的验证码是否正确，$code为用户输入的验证码字符串【在上面调用该方法】
    public function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    public function out()
    {
        session('uid',null);
        $this->success('退出登陆',u('home/index/index'));
    }
}

